import com.sun.org.apache.xpath.internal.objects.XNodeSet;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-11-29
 * Time: 19:56
 */
public class MySingleList {



    static class ListNode {
        public ListNode next;

        public int val;

        public ListNode(int val) {
            this.val = val;
        }
    }
    //判断环形链表入环节点
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }


    //判断链表是否为环形链表
    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    //求相交链表的节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null) {
            return null;
        }
        if (headB == null) {
            return null;
        }
        //1. 假设A是长的链表
        ListNode pLong = headA;
        ListNode pShort = headB;

        //2. 求各自链表长度
        int len1 = 0;
        int len2 = 0;
        while (pLong != null) {
            len1++;
            pLong = pLong.next;
        }
        while (pShort != null) {
            len2++;
            pShort = pShort.next;
        }
        //3.此时，pLong和pShort都已不在指向头节点
        //应该重新指向头节点
        //再求两个链表的长度差值，并调整长短链表的指向
        pLong = headA;
        pShort = headB;
        int len = len1 - len2;
        if (len < 0) {
            pLong = headB;
            pShort = headA;
            len = len2 - len1;
        }
        //4.长的链表引用走差值步
        while(len != 0) {
            pLong = pLong.next;
            len--;
        }
        //5.两个引用一起走
        while(pLong != pShort) {
            pLong = pLong.next;
            pShort = pShort.next;
        }
        return pLong;
    }

    //链表的回文结构
    public boolean chkPalindrome(ListNode A) {
        // write code here
        //1.找到中间节点
        ListNode fast = A;
        ListNode slow = A;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.链表逆置
        ListNode cur = slow.next;
        slow.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3.判断回文串
        ListNode head = A;
        while (head != slow && head.next != slow) {
            if (head.val != slow.val) {
                return false;
            }else {
                head = head.next;
                slow = slow.next;
            }
        }

        return true;

    }

    // 链表分割
    public ListNode partition(ListNode pHead, int x) {
        if (pHead == null) {
            return null;
        }

        // 1. 构造两个新链表---为了后序尾插简单，将链表构造成带头结点的
        ListNode bs = new ListNode(0);
        ListNode be = bs;

        ListNode as = new ListNode(0);
        ListNode ae = as;
        // 遍历链表，将小于x的节点往bs尾部插入，较大的节点往as尾部插入
        ListNode cur = pHead;
        while (cur != null) {
            if (cur.val < x) {
                be.next = cur;
                be = cur;
            }else {
                ae.next = cur;
                ae = cur;
            }
            cur = cur.next;
        }

        // 将greatHead拼接在lessHead之后形成一个链表
        be.next = as.next;
        ae.next = null;
        return bs.next;
    }

    // 合并两个有序链表
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        if (list1 == null && list2 == null) {
            return null;
        }
        ListNode newHead = new ListNode(1);
        ListNode cur = newHead;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                cur.next = list1;
                cur = list1;
                list1 = list1.next;
            }else {
                cur.next = list2;
                cur = list2;
                list2 = list2.next;
            }
        }
        if (list1 == null) {
            cur.next = list2;
        }
        if (list2 == null) {
            cur.next = list1;
        }
        return newHead.next;
    }

    public ListNode head;
    // 求链表的中间节点
    public ListNode middleNode(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        // 快慢指针法
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //单链表逆置
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }


    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        int size = size();
        if (index < 0 || index > size) {
            throw new AddIndexException("index: 位置不合法");
        }
        if (index == 0) {
            addFirst(data);
            return true;
        }
        if(index == size) {
            addLast(data);
            return true;
        }
        ListNode node = new ListNode(data);
        ListNode prev = head;
        while (index - 1 != 0) {
            prev = prev.next;
            index--;
        }
        node.next = prev.next;
        prev.next = node;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (head == null) {
            return;
        }
        if (head.val == key) {
            head = head.next;
            return;
        }
        ListNode prev = searchPrev(key);
        if (prev == null) {
            System.out.println("没有你要删除的元素!");
            return;
        }
        ListNode del = prev.next;
        prev.next = del.next;
    }
    public ListNode searchPrev(int key) {
        ListNode prev = head;
        while (prev.next != null) {
            if (prev.next.val == key) {
                return prev;
            }else {
                prev = prev.next;
            }
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = prev.next;
        while (cur != null) {
            if (cur.val == key){
                prev.next = cur.next;
            }else {
                prev = cur;

            }
            cur = cur.next;
        }
        if (head.val == key) {
            head = head.next;
        }
    }
    //得到单链表的长度
    public int size() {
        ListNode cur = head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    public void clear() {
        while (head != null) {
            ListNode headNext = head.next;
            head = null;
            head = headNext;
        }
    }
}
